from fastapi import APIRouter,Request
from fastapi.templating import Jinja2Templates
from starlette.responses import HTMLResponse
from pydantic import BaseModel

jinja2Router=APIRouter()

# 配置Jinja2模板目录
templates = Jinja2Templates(directory="templates")

class Job(BaseModel):
    id:str
    name:str

# 入参必须有request: Request变量
@jinja2Router.get("/html/{sn}",response_class=HTMLResponse)
async def html(request: Request, sn="surprise.lee"):
    ls=[
        Job(id="10010", name="架构师"),
        Job(id="10086", name="软件工程师"),
        Job(id="10050", name="测试工程师")
    ]
    return templates.TemplateResponse("index.html",{
        "request": request,
        "list": ls,
        "sn": sn
    })